package updown.controller.other;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import updown.data.SseCacheData;

/**
 * <p>Title: ww.kaixin.downupservice.controller </p>
 * <p>Description:to-22-6-1: -  </p>
 *
 * @Author:jiangkaixin
 * @Date:2021/12/23
 * @Version: 1.0
 */
@Slf4j
@Controller
public class SubscribeController {

    @Autowired
    private SseCacheData sseCacheData;

    /**
     * 订阅
     *
     * @param taskId
     * @return
     */
    @ResponseBody
    @GetMapping(path = "/subscribe")
    public SseEmitter subscribe(String taskId) {
        log.info("收到订阅:{}", taskId);
        SseEmitter sseEmitter = sseCacheData.getByTaskId(taskId);
        log.info("ssecache 中的 emmitter 是:{}",sseEmitter);
//        // 超时回调 触发
//        sseEmitter.onTimeout(() -> sseCacheData.remove(taskId));
//        // 结束之后的回调触发
//        sseEmitter.onCompletion(() -> log.info("完成！！！"));
        return sseEmitter;
    }
}
